package com.my_spring.my_spring.exception;

import com.my_spring.my_spring.pojo.ResponseMessage;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice   //  统一处理
public class GlobalExceptionHandlerAdvice {
    //  开启 日志
    Logger log = LoggerFactory.getLogger(GlobalExceptionHandlerAdvice.class);

    @ExceptionHandler({ Exception.class }) //  什么异常的统一处理
    public ResponseMessage handlerException(Exception e, HttpServletRequest request, HttpServletResponse response) {
        log.error("统一异常", e); // 打印在控制台
        return new ResponseMessage(500, "error", null);
    }
}
